Unix & Linux
Unix & Linux
contact: ludwig@univ-brest.fr
1. Why Linux ?
- Write programs that does one thing and does it well.
- Write programs that work together.
- Write programs that handle text streams, because that is a universal interface.
2. Why learn Linux basic command lines ?
- Simple : do one thing
- Flexible : built for reuse
- Versatile : can do anything/everything
- Fast : no graphics overload
- Ubiquitous : available on all machines— Permanent : First released in 1971
3. shell (command-line interpreter)
A Unix terminal is a command-line interpreter or shell that provides a traditional Unix-like command line user interface. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute, or by creating text scripts of one or more such commands.
4. bash (one type of shell, like sh, ksh, zsh,...)
bash is a command processor that typically runs in a text window, where the user types commands that cause actions.
5. Linux Shortcuts
- Previous command :
Key - Next command :
Key - Autocompletion :
(TAB) Key
- Unix/Unix:
- Select text
- middle-click (wheel button) where you want to paste
- Windows/Unix:
- Select text and Ctrl-C in Windows
- middle-click (wheel button) where you want to paste in Unix
- Unix/Windows:
- Select text in Unix
- Ctrl-V to paste in Windows
6. echo (print a message)
Command 6.1
echo
bashCommand 6.2
echo "ATGC"
bashCommand 6.3
echo -n "ATGC"
bashCommand 6.4
echo -e ">seq\nATGC"
bashCommand 6.5
echo -e ">seq\tATGC"
bashCommand 6.6
echo -e -n ">seq\nATGC"
bash7. filesystem
- 'home' (See
~) - 'root'
/
/
+-- /home/
| +-- /home/tludwig/
| +-- /home/tludwig/seqlink/
| | +-- /home/tludwig/seqlink/SEQLinkage-master/
| | +-- /home/tludwig/seqlink/SEQLinkage-master.zip
| +-- /home/tludwig/linux/
| | +-- /home/tludwig/linux/installed.pkg
| +-- /home/tludwig/file1.txt
| +-- /home/tludwig/file2.txt
+-- /etc
+-- /usr
bash8. path
- A Unix/Linux path name is a text string made up of one or more names separated by slashes
- Destination object may be file, directory, or other
.current directory..parent directory/root directory~the home directory of the current user
- Relative : the path relative to the current directory (will word from the current directory)
- Absolute : the path from the root directory (will work from anywhere)
/home/user/directory/file.txt: absolute pathdirectory/file.txt: path relative to the current directory. Same as./directory/file.txt
9. pwd (Print working directory)
Command 9.1 Where am I ?
pwd
bash10. ls (Lists files and folders)
Command 10.1
ls
bashCommand 10.2
ls .
bashCommand 10.3
ls /
bashCommand 10.4
ls ../
bashCommand 10.5
ls ../..
bashCommand 10.6
ls -l /
bashCommand 10.7 ⚠
ls does_not_exists
bashfile globbing
* means anything
Command 10.8
ls /home/* #all files and directories in /home/
bashCommande 10.9
ls /etc/*.conf #all files in /etc/ that end with .conf
bashCommand 10.10
ls -d /home/*i* #all /home subdirectories that contain i
bash11. cd (Change Directory)
Command 11.1
cd ..
bashCommand 11.2
cd .
bashCommand 11.3
cd
bashCommand 11.4
cd ~
bashCommand 11.5
cd /
bashCommand 11.6
cd -
bash12. mkdir (make directory)
Command 12.1
mkdir dir1 dir2 dir3
ls
bashCommand 12.2 ⚠
mkdir a/b/c/d
bashCommand 12.3
mkdir -p a/b/c/d
ls
ls a
ls a/b/c
bash13. cp (copy files or folders)
Command 13.1
cp /home/tludwig/master2ggb/file1.txt file1.txt
ls
bashCommand 13.2
cp file1.txt dir1
ls dir1
bashCommand 13.3
cp file1.txt dir1/file2.txt
ls dir1
bashCommand 13.4
cp dir1 dir2
bashCommand 13.5
cp -r dir1 dir2
ls dir2
bash14. rmdir (remove directory)
rmdir dir1 dir2 dir3 a
ls
bash15. rm (remove files or folders)
Command 15.1
rm file1.txt
bashCommand 15.2 ⚠
rm does_not_exits.txt
bashCommand 15.3
rm -f does_not_exits.txt
bashCommand 15.4 ⚠
rmdir a
bashCommand 15.5
rm -rf a
ls
bashCommand 15.6
rm dir1
bashCommand 15.7
rm -rf dir1 dir2
bash16. seq (Create a sequence)
Command 16.1
seq 10
bashCommand 16.2
seq 2 9
bashCommand 16.3
seq 2 3 9
bash17. cat (conCATenate files)
Preparation
cp /home/tludwig/master2ggb/file1.txt .
cp /home/tludwig/master2ggb/file2.txt .
bashCommand 17.1
cat file1.txt
bashCommand 17.2
cat file2.txt
bashCommand 17.3
cat file1.txt file2.txt
bashCommand 17.4
cat -n file1.txt file2.txt
bash18. standard input
Where the data are read from (default : keyboard ⌨)
<uses a file as standard input.
19. standard output (stdout)
Where the results are written (default : screen )
>redirects standard output to a file (and overwrites the file if it already exists).>>Appends standard output to a file.
Sans redirection
ls /root /bin
bash
Command 19.1
ls /root /bin > stdout.txt
cat stdout.txt
Command 19.2
echo "new data" > stdout.txt
cat stdout.txt
Command 19.3
ls /root ~ >> stdout.txt
cat stdout.txt
20. standard error (stderr)
Where the error messages are written (default : screen)
2>redirects standard error to a file (and overwrites the file if it already exists)
Command 20.1
ls /root /bin 2> stderr.txt
cat stderr.txt
Command 20.2
ls /root /bin > stdout.txt 2> stderr.txt
cat stdout.txt
cat stderr.txt
21. redirection (stdout | stdin )
|(pipe :AltGR+6for normal peopleAlt+Maj+Lfor Mac users) redirects standard output of a program as standard input of another.
Sans redirection
ls /
Command 21.1
ls / | cat -n
Command 21.2
echo "1601627*5" | bc
To sum up
commandA < mydata.txt # uses the content of the file named mydata.txt as the input of commandA (you probably won't use that)
commandA > output.txt # writes the results of commandA in the file named output.txt
commandA 2> message.log # writes the messages of commandA (mostly errors, but also all non results texts) in the file named message.log
commandA | commandB # passes the results of commandA as the input of commandB (called "piping")
22. wc (Word Count)
Command 22.1
cat /etc/libaudit.conf
wc /etc/libaudit.conf
bashCommand 22.2
seq 1000000 | wc -c
bashCommand 22.3
seq 1000000 | wc -l
bashCommand 22.4
echo | wc -c
bashCommand 22.5
echo -n | wc -c
bash23. stop input stdin : Ctrl-D
bc
5*5
2+9
bash⌨ <ctrl+D>
24. stop a processus Ctrl-C
seq 10000000
bash⌨<ctrl+C>
25. head (output the first part of files)
Command 25.1
seq 10000 | head
bashCommand 25.2
seq 10000 | head -3
bash26. tail (output the last part of files)
Command 26.1
seq 10000 | tail
bashCommand 26.2
seq 10000 | tail -3
bashCommand 26.3
cat /home/tludwig/master2ggb/table.tsv
bashCommand 26.4
cat /home/tludwig/master2ggb/table.tsv | tail -n+2
bash27. paste (merge lines of files)
Preparation
seq 1 10 > a
cat a
seq 2 12 > b
cat b
bashCommand 27.1
paste a b
bashCommand 27.2
seq 20 | paste - -
bashCommand 27.3
seq 20 | paste - - -
bash28. cut (extract sections from each line of files)
Without cut
seq 1 100 | paste - - - - -
bashCommand 28.1
seq 1 100 | paste - - - - - | cut -f 3
bashCommand 28.2
seq 1 100 | paste - - - - - | cut -f 3-
bashCommand 28.3
seq 1 100 | paste - - - - - | cut -f 2-4
bashCommand 28.4
seq 1 100 | paste - - - - - | cut -f -2
bashCommand 28.4
seq 1 100 | paste - - - - - | cut -f 1,5
bashCommand 28.5
echo "ABCDE" | cut -c 2
bashCommand 28.6
echo "ABCDE" | cut -c 2,5
bashCommand 28.7
echo "ABCDE" | cut -c 2-5
bashCommand 28.8
echo "AA:BB:CC:DD:EE:FF" | cut -d':' -f2
bash29. sort (sort lines of text files)
Command 29.1
seq 1 25 | sort
bashCommand 29.2
seq 1 25 | sort -n
bashCommand 29.3
seq 1 25 | sort -n -r
bashCommand 29.4
cat /home/tludwig/master2ggb/chrom.list
bashCommand 29.5
cat /home/tludwig/master2ggb/chrom.list | sort
bashCommand 29.6
cat /home/tludwig/master2ggb/chrom.list | sort -n
bashCommand 29.7
cat /home/tludwig/master2ggb/chrom.list | sort -V
bashCommand 29.8
cat /home/tludwig/master2ggb/table.tsv
bashCommand 29.9
cat /home/tludwig/master2ggb/table.tsv | tail -n+2 | sort -t $'\t' -k2n
bashCommand 29.10
cat /home/tludwig/master2ggb/table.tsv | tail -n+2 | sort -t $'\t' -k3r -k2n
bash30. uniq (report or omit repeated lines)
Without uniq
echo -e "A\nA\nA\nB\nB\nA"
bashCommand 30.1
echo -e "A\nA\nA\nB\nB\nA" | uniq
bashCommand 30.2
echo -e "A\nA\nA\nB\nB\nA" | sort | uniq
bashCommand 30.3
echo -e "A\nA\nA\nB\nB\nA" | sort -u
bashCommand 30.4
echo -e "A\nA\nA\nB\nB\nA" | sort | uniq -c
bashCommand 30.5
echo -e "A\nA\nA\nB\nB\nA" | sort | uniq -c | sort -n
bash31. grep (print lines matching a pattern
Without grep
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT"
bashCommand 31.1
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep AT
bashCommand 31.2
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep GAATTC
bashCommand 31.3
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -v ">"
bashCommand 31.4
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -B1 GAATTC
bashCommand 31.5
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -A1 gene2
bashCommand 31.6
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -E '(GAATTC|CCT)'
bashCommand 31.7
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -o A
bashCommand 31.8
echo -e ">gene1\nATGGAATTCAAAA\n>gene2\nCCCTGCTGATCGATCGATCGT" | grep -o A | wc -l
bash32. more / less (file perusal)
seq 1000000 | more
seq 1000000 | less
bash<enter>to skip 1 line<space>to skip 1 pageqto quit- arrows in
lessbut not inmore
33. touch (Change file timestamp, or create a file)
touch new_file
ls -l
bash34. find (search for files in a directory hierarchy)
Command 34.1
find ~ -type f
bashCommand 34.2
find /etc -type f -name "*.conf"
bash35. tr (Translate, squeeze, and/or delete characters)
Command 35.1
echo "ATAAAAACGACTTTTGA" | tr "T" "U"
bashCommand 35.2
echo "ATAAAAACGACTTTTGA" | tr -s "A"
bashCommand 35.3
echo "ATAAAAACGACTTTTGA" | tr -d "A"
bashCommand 35.4
echo "ATAAAAACGACTTTTGA" | tr "[ATGC]" "[TACG]"
bash36. rev (reverse lines characterwise)
echo "ATAAAAACGACTTTTGA" | tr "[ATGC]" "[UACG]" | rev
bash37. gzip (compress files)
Without gzip
seq 100000 | wc -c
bashCommand 37.1
seq 100000 | gzip | wc -c
bashWithout gzip
cat file1.txt
bashCommand 37.2
gzip file1.txt
cat file1.txt.gz
bash38. gunzip / zcat (expand files)
Command 38.1
zcat file1.txt.gz
bashCommand 38.2
gunzip file1.txt.gz
bashCommand 38.3
seq 10 | gzip | gunzip
bash39. diff / sdiff (compare files line by line)
Preparation
seq 20 > a
seq 10 30 > b
bashCommand 39.1
diff a b
bashCommand 39.2
sdiff a b
bash